<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="UTF-8">
    <title>商品后台管理</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" />
    <link rel="stylesheet" href="./css/bootstrap.css">
    <link rel="stylesheet" href="./css/all.css">
    <link rel="stylesheet" href="./css/course_category.css">
</head>

<body>
    <div id="app">
        <nav class="navbar">
            <div class="container" style="height: 80px;background: #58a3f9">
                <!-- Brand and toggle get grouped for better mobile display -->
                <div class="navbar-header">
                    <a class="navbar-brand" href="#">
                        <img src="image/easy-pay.png">
                    </a>
                </div>
                <ul class="nav navbar-nav navbar-right" style="margin-top: 30px">
                    <li><a href="#"><span class="inner">首页</span></a></li>
                    <li>
                        <a href="#">
                            <span class="inner">商品管理</span></a>
                    </li>
                    <li>
                        <a href="#">
                            <span class="inner">购物车管理</span>
                        </a>
                    </li>

                    <li>
                        <a href="#">
                            <span class="inner">用户管理</span></a>
                    </li>
                </ul>
            </div>
        </nav>
        <div style="min-height: 400px">
            <div class="container">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h4>商品管理-修改商品</h4>
                    </div>
                    <div class="modal-body" id="modalbody">
                        
                    </div>
                </div>
            </div>
        </div>
        <footer class="container">
            <p class="text-center">易购集团</p>
            <p class="text-center">Copyright 2020</p>
        </footer>
    </div>
</body>

</html>
<!--  提示，本题需要表单验证
如：单价必须输入纯数值，单价不能为负数
单价只可以是1-2位小数
/^(([1-9]\d+)|0)(\.\d{1,2})?$/.test()
 -->
<script src="./js/axios.min.js"></script>
<script>
  let curId = localStorage.getItem('curId')
    axios.post("http://localhost:8090/h10/goods/getGoods",{"id":1})
    .then(res=>{
        console.log(res.data);
        if(res.data.success){
            const modalbody=document.getElementById("modalbody")
            modalbody.innerHTML=`
            <form class="form-horizontal">

<div class="form-group">
    <label for="id" class="col-sm-3 control-label">商品编号</label>
    <div class="col-sm-8">
        <input type="text" class="form-control" id="id" value="${res.data.goods.id}">
    </div>
</div>
<div class="form-group">
    <label for="title" class="col-sm-3 control-label">商品标题</label>
    <div class="col-sm-8">
        <input type="text" class="form-control" id="title" value="${res.data.goods.title}">
    </div>
    <div class="col-sm-1 red">*</div>
</div>
<div class="form-group">
    <label for="content" class="col-sm-3 control-label">商品描述</label>
    <div class="col-sm-8">
        <textarea class="form-control" rows="3" style="resize: none"
            id="content">${res.data.goods.content}</textarea>
    </div>
</div>
<div class="form-group">
    <label for="imagePath" class="col-sm-3 control-label">图片路径</label>
    <div class="col-sm-8">
        <input type="text" class="form-control" id="imagePath" value="${res.data.goods.imagePath}">
    </div>
    <div class="col-sm-1 red">*</div>
</div>
<div class="form-group">
    <label for="price" class="col-sm-3 control-label">商品价格</label>
    <div class="col-sm-8">
        <input type="text" class="form-control" id="price" value="${res.data.goods.price}">
    </div>
    <div class="col-sm-1 red">*</div>
</div>
<div class="form-group">
    <div class="col-sm-8 col-sm-offset-3">
        <p class="form-control-static text-danger" id="msg">编号为${res.data.goods.id}的商品已取出</p>
    </div>
</div>
<div style="text-align: right">
    <button type="button" class="btn btn-primary" id="assure"  onclick="updatagooda()">确定</button>
    <a href="../06题商品管理模块商品列表功能/index.html">
    <button type="button" class="btn btn-default">取消</button></a>
</div>
</form>

`

        }
    })

    const assure=document.getElementById("assure")
    function updatagooda(){
    const id=document.getElementById('id').value
    const title=document.getElementById('title').value
    const content=document.getElementById('content').value
    const imagePath=document.getElementById('imagePath').value
    const price=document.getElementById('price').value
      if(title===""){
            msg.innerHTML="商品标题不能为空"
            return false
        }
        if(content===''){
            msg.innerHTML="商品图片不能空"
            return false
        }
        if(price===''){
            msg.innerHTML="商品单价不能为空"
            return false
        }
        if(!/^(([1-9]\d+)|0)(\.\d{1,2})?$/.test(price)){
            msg.innerHTML="商品价格必须为合法数字"
            return false
        }
        axios.post('http://localhost:8090/h10/goods/updateGoods',{id,title,content,imagePath,price})
        .then(res=>{
            msg.innerHTML="修改成功";
        })
    }

   
    

</script>